package controllers

import (
	"xxp_api/models"

	"github.com/astaxie/beego"
	//"github.com/astaxie/beego/logs"
)

// 用户相关接口
type UserController struct {
	beego.Controller
}

// @Summary 小程序授权登录接口
// @Description 微信登录
// @Param	code 		    query 	string	true	 "用户登录code"
// @Param	nickname 	    query 	string	true	 "用户登录昵称"
// @Param	headimgurl 		query 	string	true	 "用户登录头像"
// @Param	invitation 		string	true	 "邀请码"
// @Success 200 {object} models.Response
// @router /login [post]
func (u *UserController) Login() {
	var ret models.Response

	code := u.GetString("code")
	nickname := u.GetString("nickname")
	headimgurl := u.GetString("headimgurl")
	invitation := u.GetString("invitation")
	beego.Debug(u.Ctx.Request.Form)
	if nickname == "" || code == "" || headimgurl == "" {
		beego.Debug("参数错误nickname:", nickname, ",code:", code, ",headimgurl:", headimgurl)
		ret.Code = models.ERRCODE_PARAMS_ERROR
		ret.Msg = "参数错误"
	} else {
		beego.Debug(code, nickname, headimgurl, invitation)
		ret = models.GetWechatInfo(code, nickname, headimgurl, invitation)
	}

	u.Data["json"] = ret
	u.ServeJSON()
}

// @Summary  设置身份证信息接口
// @Description 设置姓名身份证号码
// @Param	uid 		query 	int	true	 "uid"
// @Param	name 	    query 	string	true	 "身份证名字"
// @Param	idcard 		query 	string	true	 "省份证号码"
// @Success 200 {object} models.Response
// @router /setidcard [post]
func (u *UserController) Setidcard() {
	var ret models.Response

	uid, err := u.GetInt("uid")
	name := u.GetString("name")
	idcard := u.GetString("idcard")
	beego.Debug(u.Ctx.Request.Form)
	if name == "" || idcard == "" || err != nil || uid <= 0 {
		beego.Debug("参数错误Name:", name, ",idcard:", idcard, ",uid:", uid)
		ret.Code = models.ERRCODE_PARAMS_ERROR
		ret.Msg = "参数错误"
	} else {
		ret = models.Setidcard(uid, name, idcard)
	}

	u.Data["json"] = ret
	u.ServeJSON()
}

// @Summary  kol我的页面接口
// @Description 我的页面
// @Param	uid 		query 	int	true	 "uid"
// @Success 200 {object} models.Response
// @router /my [post]
func (u *UserController) My() {
	var ret models.Response

	uid, err := u.GetInt("uid")
	beego.Debug(u.Ctx.Request.Form)
	if err != nil || uid <= 0 {
		beego.Debug("参数错误uid:", uid)
		ret.Code = models.ERRCODE_PARAMS_ERROR
		ret.Msg = "参数错误"
	} else {
		ret = models.KolMy(uid)
	}

	u.Data["json"] = ret
	u.ServeJSON()
}

// @Summary  我的余额
// @Description 我的余额
// @Param	uid 		query 	int	true	 "uid"
// @Success 200 {object} models.Response
// @router /balance [post]
func (u *UserController) Balance() {
	var ret models.Response

	uid, err := u.GetInt("uid")
	beego.Debug(u.Ctx.Request.Form)
	if err != nil || uid <= 0 {
		beego.Debug("参数错误uid:", uid)
		ret.Code = models.ERRCODE_PARAMS_ERROR
		ret.Msg = "参数错误"
	} else {
		ret = models.KolBalance(uid)
	}

	u.Data["json"] = ret
	u.ServeJSON()
}

// @Summary  获取提现手续费和免费额度
// @Description 获取提现手续费和免费额度
// @Param	uid 		query 	int	true	 "uid"
// @Success 200 {object} models.Response
// @router /quota [post]
func (u *UserController) Quota() {
	var ret models.Response
	uid, err := u.GetInt("uid")
	beego.Debug(u.Ctx.Request.Form)
	if err != nil || uid <= 0 {
		beego.Debug("参数错误uid:", uid)
		ret.Code = models.ERRCODE_PARAMS_ERROR
		ret.Msg = "参数错误"
	} else {
		ret = models.KolQuota(uid)
	}

	u.Data["json"] = ret
	u.ServeJSON()
}

// @Summary 提现手机号验证
// @Description 提现手机号验证
// @Param	 uid 		query 	int	true	 "uid"
// @Success 200 {object} models.Response
// @router /sms [post]
func (u *UserController) Sms() {
	var ret models.Response
	uid, err := u.GetInt("uid")
	//phone := u.GetString("phone")
	beego.Debug(u.Ctx.Request.Form)
	if err != nil || uid <= 0 {
		ret.Code = models.ERRCODE_PARAMS_ERROR
		ret.Msg = "参数错误"
	} else {
		ret = models.KolSms(uid)
	}

	u.Data["json"] = ret
	u.ServeJSON()
}

// @Summary 提现接口
// @Description 提现接口
// @Param	uid 	   query 	int	true	 "uid"
// @Param	amount 	   query 	float	true	 "提现金额"
// @Param   code       query  string true  "验证码"
// @Param   card       query  string true  "银行卡"
// @Param   name       query  string true  "姓名"
// @Param   bank       query  string true  "银行名称"
// @Param   branch     query  string true  "银行支行"
// @Success 200 {object} models.Response
// @router /withdraw [post]
func (u *UserController) Withdraw() {
	var ret models.Response
	uid, err := u.GetInt("uid")
	amount, err2 := u.GetFloat("amount")
	code := u.GetString("code")
	card := u.GetString("card")
	name := u.GetString("name")
	bank := u.GetString("bank")
	branch := u.GetString("branch")

	beego.Debug(u.Ctx.Request.Form)
	if err != nil || uid <= 0 || code == "" || card == "" || name == "" || bank == "" || branch == "" || amount <= 0 || err2 != nil {
		beego.Debug("参数错误uid:", uid)
		ret.Code = models.ERRCODE_PARAMS_ERROR
		ret.Msg = "参数错误"
	} else {
		ret = models.KolWithdraw(uid, amount, code, card, name, bank, branch)
	}

	u.Data["json"] = ret
	u.ServeJSON()
}

// @Summary 收支明细
// @Description 收支明细
// @Param	 uid 		 query 	int	true	 "uid"
// @Param   types       query  string true  "0 全部 1已提现，2未通过，3审核中，4返利结算"
// @Param	keystart 	    int	    true	 "分页参数"
// @Success 200 {object} models.Response
// @router /balancedetail [post]
func (u *UserController) Balancedetail() {
	var ret models.Response
	uid, err := u.GetInt("uid")
	types, _ := u.GetInt("types")
	keystart, _ := u.GetInt("keystart")

	beego.Debug(u.Ctx.Request.Form)
	if err != nil || uid <= 0 || types < 0 {
		ret.Code = models.ERRCODE_PARAMS_ERROR
		ret.Msg = "参数错误"
	} else {
		ret = models.KolBalanceDetail(uid, types, keystart)
	}

	u.Data["json"] = ret
	u.ServeJSON()
}

// @Summary 绑定手机号
// @Param	 uid 		query 	int	true	 "uid"
// @Param   phone      query   string    "手机号码"
// @Success 200 {object} models.Response
// @router /bindphone [post]
func (u *UserController) Bindphone() {
	var ret models.Response
	uid, err := u.GetInt("uid")
	phone := u.GetString("phone")
	beego.Debug(u.Ctx.Request.Form)
	if err != nil || uid <= 0 {
		ret.Code = models.ERRCODE_PARAMS_ERROR
		ret.Msg = "参数错误"
	} else {
		ret = models.KolBindPhone(uid, phone)
	}

	u.Data["json"] = ret
	u.ServeJSON()
}

// @Summary 保存手机号
// @Param	 uid 		query 	int	true	 "uid"
// @Param   phone      query   string    "手机号码"
// @Param   code      query   string    "验证码"
// @Param   invitation      query   string    "邀请码"
// @Success 200 {object} models.Response
// @router /savephone [post]
func (u *UserController) Savephone() {
	var ret models.Response
	uid, err := u.GetInt("uid")
	phone := u.GetString("phone")
	code := u.GetString("code")
	invitation := u.GetString("invitation")
	beego.Debug(u.Ctx.Request.Form)
	if err != nil || uid <= 0 {
		ret.Code = models.ERRCODE_PARAMS_ERROR
		ret.Msg = "参数错误"
	} else {
		ret = models.KolSavePhone(uid, phone, code, invitation)
	}

	u.Data["json"] = ret
	u.ServeJSON()
}
